﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class SelectMove : MonoBehaviour
{
    private List<Transform> selectionList;
    private void Start()
    {
        selectionList = new List<Transform>(5);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            //构造射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))//检测射线
            {
                //看下碰到什么东西
                if (hitInfo.collider.tag == "Plane")
                {
                    //让选中的移动
                    for (int i = 0; i < selectionList.Count; i++)
                    {
                        selectionList[i].GetComponent<NavMeshAgent>().SetDestination(hitInfo.point);
                    }
                }
                else if (hitInfo.collider.tag == "Monster")
                {
                    //做选择
                    ChangeSelection(hitInfo.transform);
                }
            }
        }
    }

    //改变选择
    public void ChangeSelection(Transform tFthisClick)
    {
        if (Input.GetKey(KeyCode.LeftControl))//按住Ctrl
        {
            if (selectionList.Contains(tFthisClick))    //存在就移除
            {
                tFthisClick.ChangeColor(Color.white);
                selectionList.Remove(tFthisClick);
            }
            else  //集合中不存在tFthisClick就放进去
            {
                tFthisClick.ChangeColor(Color.red);
                selectionList.Add(tFthisClick);
            }
        }
        else
        {
            //颜色都变回白色
            for (int i = 0; i < selectionList.Count; i++)
            {
                selectionList[i].ChangeColor(Color.white);
            }
            //移除
            selectionList.Clear();

            //把这次点击
            tFthisClick.ChangeColor(Color.red);
            selectionList.Add(tFthisClick);
        }
    }
}